//////////////////////////////////////////////////////////////////////////////////
//                                                                              //
//  This file is part of the buola project (https://code.google.com/p/buola/).  //
//                                                                              //
//  Copyright(c) 2007-2012 Xavi Gratal                                          //
//  gratal AT gmail DOT com                                                     //
//                                                                              //
//  Buola is free software: you can redistribute it and/or modify               //
//  it under the terms of the GNU General Public License as published by        //
//  the Free Software Foundation, either version 3 of the License, or           //
//  (at your option) any later version.                                         //
//                                                                              //
//  Buola is distributed in the hope that it will be useful,                    //
//  but WITHOUT ANY WARRANTY; without even the implied warranty of              //
//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the               //
//  GNU General Public License for more details.                                //
//                                                                              //
//  You should have received a copy of the GNU General Public License           //
//  along with buola.  If not, see <http://www.gnu.org/licenses/>.              //
//                                                                              //
//////////////////////////////////////////////////////////////////////////////////

#include <buola/bbox/csession.h>

namespace buola { namespace bbox {

CSession::CSession()
{
}

CSession::~CSession()
{
}

void CSession::RegisterService(detail::CServiceBase *pBase,const std::string &pAddress)
{
    mDaemonConnection.RegisterService(pAddress,pBase->BBoxUID());
    mLocalServices[pBase->BBoxUID()]=pBase;
}

BServiceProxy CSession::GetService(const std::string &pAddress)
{
    std::pair<uint64_t,uint64_t> lIDs=mDaemonConnection.GetService(pAddress);
    if(lIDs.first==(uint64_t)-1) throw XNotFound("service "+pAddress+" not found");
    return BServiceProxy(GetPeer(lIDs.first),lIDs.second);
}

protocol::PPeerConnection CSession::GetPeer(uint64_t pID)
{
    auto i=mPeerConnections.find(pID);
    if(i!=mPeerConnections.end())
        return i->second;
    
    io::PSocket lSocket=mDaemonConnection.ConnectToClient(pID);
    if(!lSocket) throw XConnection("can't connect to remote peer");
    protocol::PPeerConnection lPeer=new protocol::CPeerConnection(std::move(lSocket));
    mPeerConnections.emplace(pID,lPeer);
    return lPeer;
}

void CSession::OnClientConnection(uint64_t pOther,io::PSocket pSocket)
{
    auto i=mPeerConnections.find(pOther);
    if(i!=mPeerConnections.end())
        msg_error() << "connection already exists in OnClientConnection... should fix this so that it never happens\n";
    
    protocol::PPeerConnection lPeer=new protocol::CPeerConnection(std::move(pSocket));
    mPeerConnections.emplace(pOther,lPeer);
}

detail::CServiceBase *CSession::GetLocalService(uint64_t pID)
{
    auto i=mLocalServices.find(pID);
    if(i==mLocalServices.end())
        throw XNotFound("can't find local service");
    return i->second;
}

CSession &get_session()
{
    static CSession *lSession=new CSession;

    return *lSession;
}

/*namespace bbox*/ } /*namespace buola*/ }
